var iFDU = require('../../utils/iFDU.js');
var app = getApp();
Page({

  data: {
    config: app.config,
    globalData: app.globalData,
    virtualRedbagId: 0,
    virtualRedbagInfo: {},
    receiveRedbagResult: -1,
    wxNameInSharePerson: '',
    errMsg: {
      1: {
        title: '红包已失效',
        msg: '该红包已失效或已过期！'
      },
      2: {
        title: '不要贪心哦',
        msg: '你已领取过这个红包！'
      },
      3: {
        title: '手慢了',
        msg: '很遗憾，该红包已被抢完！'
      }
    }
  },

  showQRCode: function () {
    wx.navigateTo({
      url: '../qrcode/qrcode',
    })
  },

  onLoad: function (options) {
    var that = this;
    that.setData({
      virtualRedbagId: options.virtualRedbagId
    });

    app.getUserInfo(function (globalData) {
      that.setData({
        globalData: globalData
      });

      iFDU.api.doGet(
        'redbag/getVirtualRedbagById',
        {
          userId: globalData.userId,
          virtualRedbagId: that.data.virtualRedbagId,
        },
        function (redbag) {
          iFDU.api.doGet(
            'redbag/receiveRedbag',
            {
              userId: globalData.userId,
              redbagKindId: redbag.virtualRedbagView.redbag_kind_id,
              userIdInSharePerson: redbag.virtualRedbagView.user_id
            },
            function (data) {
              that.setData({
                virtualRedbagInfo: redbag.virtualRedbagView,
                receiveRedbagResult: data.result,
                virtualRedbagType: data.type,
                wxNameInSharePerson: data.wxNameInSharePerson
              })
            }
          )
        }
      )
    });
  },

  onShareAppMessage: function (res) {
    var that = this;
    return {
      title: '你的好友发来一份红包，猛戳领取！',
      imageUrl: 'http://s.i-fdu.com/runto/wxa/s/share2.jpg',
      path: 'pages/redbag-detail/redbag-detail?virtualRedbagId=' + that.data.virtualRedbagId,
      success: function (res) {
      },
      fail: function (res) {
      }
    }
  },

  goHome: function () {
    wx.switchTab({
      url: '../home/home',
    })
  }
})